package com.fuhe.studydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;

public class CountActivity extends Activity {

    private Button button_to_do;
    private TextView text_count;
    private boolean isStarted =false;
    private int mCount = 0;
    private Handler handler = new Handler();

    private Runnable mCounter = new Runnable() {
        @Override
        public void run() {
            mCount++;
            text_count.setText("当前计数值为: "+mCount);
            handler.postDelayed(this,100);
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counting);

        initView();
    }

    private void initView() {
        button_to_do = findViewById(R.id.button_to_do);
        text_count = findViewById(R.id.text_count);

        button_to_do.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isStarted){
                    button_to_do.setText("停止计数");
                    handler.post(mCounter);
                }else {
                    button_to_do.setText("开始计数");
                    handler.removeCallbacks(mCounter);
                }
                isStarted = !isStarted;
            }
        });
    }


}
